﻿using Sc.Domain;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sc.Persistence
{
    public class TrainDAO
    {
        #region  BasicMethod

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Train model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Trains(");
            strSql.Append("BaseItemId,Time,UserId,OrgzNo,TrainCount)");
            strSql.Append(" values (");
            strSql.Append("@BaseItemId,@Time,@UserId,@OrgzNo,@TrainCount)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@BaseItemId", SqlDbType.Int,4),
					new SqlParameter("@Time", SqlDbType.Date),
					new SqlParameter("@UserId", SqlDbType.Int,4),
					new SqlParameter("@OrgzNo", SqlDbType.Int,4),
                    new SqlParameter("@TrainCount", SqlDbType.Int,4)};
            parameters[0].Value = model.BaseItemId;
            parameters[1].Value = model.Time;
            parameters[2].Value = model.UserId;
            parameters[3].Value = model.OrgzNo;
            parameters[4].Value = model.TrainCount;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Train model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Trains set ");
            strSql.Append("BaseItemId=@BaseItemId,");
            strSql.Append("Time=@Time,");
            strSql.Append("TrainCount=@TrainCount");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@BaseItemId", SqlDbType.Int,4),
					new SqlParameter("@Time", SqlDbType.Date,3),
					new SqlParameter("@TrainCount", SqlDbType.Int),
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = model.BaseItemId;
            parameters[1].Value = model.Time;
            parameters[2].Value = model.TrainCount;
            parameters[3].Value = model.Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Trains ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Trains ");
            strSql.Append(" where Id in (" + idlist + ")  ");
            List<string> list = new List<string>();
            list.Add(strSql.ToString());
            StringBuilder str = new StringBuilder();
            str.Append("delete from TrainDetails ");
            str.Append(" where TrainId in (" + idlist + ")  ");
            list.Add(str.ToString());
            int rows = DbHelperSQL.ExecuteSqlTran(list);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Train DataRowToModel(DataRow row)
        {
            Train model = new Train();
            if (row != null)
            {
                if (row["Id"] != null && row["Id"].ToString() != "")
                {
                    model.Id = int.Parse(row["Id"].ToString());
                }
                if (row["BaseItemId"] != null && row["BaseItemId"].ToString() != "")
                {
                    model.BaseItemId = int.Parse(row["BaseItemId"].ToString());
                    model.BaseItemName = row["Name"].ToString();
                }
                if (row["Time"] != null && row["Time"].ToString() != "")
                {
                    model.Time = DateTime.Parse(row["Time"].ToString());
                }
                if (row["UserId"] != null && row["UserId"].ToString() != "")
                {
                    model.UserId = int.Parse(row["UserId"].ToString());
                }
                if (row["CreateTime"] != null && row["CreateTime"].ToString() != "")
                {
                    model.CreateTime = DateTime.Parse(row["CreateTime"].ToString());
                }
                if (row["OrgzNo"] != null && row["OrgzNo"].ToString() != "")
                {
                    model.OrgzNo = int.Parse(row["OrgzNo"].ToString());
                    model.OrgzName = row["OrgzName"].ToString();
                }
                if (row["TrainCount"] != null && row["TrainCount"].ToString() != "")
                {
                    model.TrainCount = int.Parse(row["TrainCount"].ToString());
                }
            }
            return model;
        }

        #endregion  BasicMethod
    }
}
